<template>
    <div class="divBox">
      <el-card>
        <div class="mb20">
          <el-button size="small" type="primary" @click="onEdit">创建套餐</el-button>
        </div>
        <el-table
          v-loading="listLoading"
          :data="tableData.data"
          size="small"
          highlight-current-row
        >
            <el-table-column
            prop="id"
            label="ID"
            min-width="60"
            />
          <el-table-column
            label="名称"
            min-width="60"
            prop="name"
          >
            <!-- <template slot-scope="{row}">
              <span v-text="$route.path.indexOf('group') !== -1?row.group_id:row.label_id" />
            </template> -->
          </el-table-column>
          
          <el-table-column
          prop="price"
          label="价格"
          min-width="60"
          />
          <el-table-column
          prop="total_number"
          label="总桶数"
          min-width="60"
          />
          <el-table-column
          prop="send_per"
          label="送水费"
          min-width="60"
          />
        
      <el-table-column
        prop="return_per"
        label="每桶返还"
        min-width="60"
      />
        <el-table-column label="操作" min-width="90" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="onEdit(scope.row)">编辑</el-button>
              <el-button type="text" size="small" @click="handleDelete(scope.row.id, scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- <div class="block">
          <el-pagination
            background
            :page-size="tableFrom.limit"
            :current-page="tableFrom.page"
            layout="total, prev, pager, next, jumper"
            :total="tableData.total"
            @size-change="handleSizeChange"
            @current-change="pageChange"
          />
        </div> -->
      </el-card>

      <!-- 套餐编辑、创建 -->
      <pack-edit
        @closeDrawer="closeDrawer"
        @getList="getList"
        :createDrawer="createDrawer" 
        :packData="rowData"
        ref="packEdit" 
        />
    </div>
  </template>
  
  <script>
  
  import {
    groupLstApi,
    groupFormApi,
    groupEditApi,
    groupDeleteApi,
    labelLstApi,
    labelFormApi,
    labelEditApi,
    labelDeleteApi,
    packagelst,
    packDeleteApi
  } from '@/api/user'
  import packEdit from './packEdit'
  export default {
    name: 'packagelst',
    data() {
      return {
        tableFrom: {
          page: 1,
          limit: 20
        },
        tableData: {
          data: [],
          total: 0
        },
        listLoading: true,
        createDrawer:false,//创建,编辑套餐
        rowData:{}
      }
    },
    components: {
        packEdit
    },

    mounted() {
      this.getList()
    },
    watch:{
      '$route.path': {   
        handler: function() {
          this.getList()
        },
        immediate: false,
        deep: true
      },
    },
    methods: {
      // 列表
      getList() {
        this.listLoading = true
        // this.$route.path.indexOf('group') !== -1 ? groupLstApi(this.tableFrom).then(res => {
            packagelst(this.tableFrom).then(res => {
          this.tableData.data = res.data
          this.tableData.total = res.data.count
          this.listLoading = false
        }).catch(res => {
          this.listLoading = false
          this.$message.error(res.message)
        })
      },
      pageChange(page) {
        this.tableFrom.page = page
        this.getList()
      },
      handleSizeChange(val) {
        this.tableFrom.limit = val
        this.getList()
      },
      // 添加
      onAdd() {
        this.$modalForm(this.$route.path.indexOf('group') !== -1 ? groupFormApi() : labelFormApi()).then(() => this.getList())
      },
      // 编辑
      onEdit(row) {
        if(row.name){
            this.rowData = JSON.parse(JSON.stringify(row));
        }else{
            this.rowData = {id:null};
        }
        this.createDrawer = true;
        if(row.name){
            this.$refs.packEdit.setData(JSON.parse(JSON.stringify(row)));
        }
        // this.$modalForm(this.$route.path.indexOf('group') !== -1 ? groupEditApi(id) : labelEditApi(id)).then(() => this.getList())
      },
      // 删除
      handleDelete(id, idx) {
        this.$modalSure('删除该套餐').then(() => {
          packDeleteApi(id).then(({ message }) => {
            this.$message.success(message)
            this.tableData.data.splice(idx, 1)
            this.getList()
          }).catch(({ message }) => {
            this.$message.error(message)
          })
        })
      },
      closeDrawer() {
            this.drawer = false
            this.createDrawer = false;
        },
    }
  }
  </script>
  
  <style scoped lang="scss">
  
  </style>
  